package com.eidos.blogs.openapi.service.seckill.impl;

import com.eidos.blogs.openapi.entity.seckill.RedeInfo;
import com.eidos.blogs.openapi.exception.SourceException;
import com.eidos.blogs.openapi.rest.QueryParams;
import com.eidos.blogs.openapi.mapper.seckill.RedeInfoMapper;
import com.eidos.blogs.openapi.service.seckill.IRedeInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author ： Eidos
 * @date ：Created in 2021-01-12
 * @description：红包信息表 服务实现类
 * @modified By：
 * @version: 1.0
 */
@Service
public class RedeInfoServiceImpl extends ServiceImpl<RedeInfoMapper, RedeInfo> implements IRedeInfoService {

    @Autowired
    private RedeInfoMapper redeInfoMapper;

    /**
     * 红包信息表分页列表
     *
     * @param param 根据需要进行传值
     * @return
     */
    @Override
    public IPage<RedeInfo> page(RedeInfo param) {
        Page page = QueryParams.getPage();
        QueryWrapper<RedeInfo> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                // 自增主键
                .eq(param.getId() != null, RedeInfo::getId, param.getId())
                // 红包唯一ID
                .eq(param.getRedEnvelopeId() != null, RedeInfo::getRedEnvelopeId, param.getRedEnvelopeId())
                // userid
                .eq(param.getUserId() != null, RedeInfo::getUserId, param.getUserId())
                // 标题
                .eq(!StringUtils.isEmpty(param.getTitle()), RedeInfo::getTitle, param.getTitle())
                // 红包金额单位分
                .eq(param.getTotalAmount() != null, RedeInfo::getTotalAmount, param.getTotalAmount())
                // 红包个数
                .eq(param.getTotalNum() != null, RedeInfo::getTotalNum, param.getTotalNum())
                // 红包类型1手气红包2平均红包
                .eq(param.getType() != null, RedeInfo::getType, param.getType())
                // 0抢完1未抢完
                .eq(param.getStatus() != null, RedeInfo::getStatus, param.getStatus())
                // 0不开启1全中2全不中
                .eq(param.getOpenHit() != null, RedeInfo::getOpenHit, param.getOpenHit())
                // 开启全不中避开的数字
                .eq(!StringUtils.isEmpty(param.getHitNumber()), RedeInfo::getHitNumber, param.getHitNumber())
                // 创建时间
                .eq(param.getCreateTime() != null, RedeInfo::getCreateTime, param.getCreateTime())
        ;
        return baseMapper.selectPage(page, wrapper);
    }

    /**
     * 红包信息表详情
     *
     * @param id
     * @return
     */
    @Override
    public RedeInfo info(Long id) {
        return getById(id);
    }

    /**
     * 红包信息表新增
     *
     * @param param 根据需要进行传值
     * @return
     */
    @Override
    public void add(RedeInfo param) {
        if (!save(param)) {
            throw new SourceException("新增失败");
        }
    }

    /**
     * 红包信息表修改
     *
     * @param param 根据需要进行传值
     * @return
     */
    @Override
    public void modify(RedeInfo param) {
        if (!updateById(param)) {
            throw new SourceException("保存失败");
        }
    }

    /**
     * 红包信息表删除(单个条目)
     *
     * @param id
     * @return
     */
    @Override
    public void remove(Long id) {
        removeById(id);
    }

    /**
     * 红包信息表删除(多个条目)
     *
     * @param ids
     * @return
     */
    @Override
    public void removes(List<Long> ids) {
        removeByIds(ids);
    }

}
